<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Category_model extends MY_Model
{
    private $_category_map = array ();
    private $_category_group = array ();
    function load($gid)
    {
        $this->_db->select( $this->_table, '`id`,`fid`,`img_cover`,`list_uri`,`title`,`subtitle`,`ext`' );
        $this->_db->where( '`gid`=? and `deleted`=0', $gid );
        $this->_db->order( '`sort` desc,`id`' );
        $category = $this->_db->fetch_all();
        foreach ( $category as &$item )
        {
            $this->_category_map[$item['id']] = &$item;
            $this->_category_group[$item['fid']][] = &$item;
        }
        return ! empty( $this->_category_group );
    }
    function get_current($cid)
    {
        if (isset( $this->_category_map[$cid] ))
        {
            return $this->_category_map[$cid];
        }
        return $this->_category_group[0][0];
    }
    function get_path($cid, $self = true)
    {
        if (isset( $this->_category_map[$cid] ))
        {
            $father_path = $this->get_path( $this->_category_map[$cid]['fid'], true );
            if ($self)
            {
                $father_path[] = $this->_category_map[$cid];
            }
            return $father_path;
        }
        return array ();
    }
    function get_ids($cid = 0)
    {
        $ids = array ();
        if (empty( $cid ))
        {
            foreach ( $this->_category_group[0] as $item )
            {
                $ids = array_merge( $ids, $this->get_ids( $item['id'] ) );
            }
            if (empty( $ids ))
            {
                $ids[] = 0;
            }
            return $ids;
        }
        $ids[] = $cid;
        if (isset( $this->_category_group[$cid] ))
        {
            foreach ( $this->_category_group[$cid] as $item )
            {
                $ids = array_merge( $ids, $this->get_ids( $item['id'] ) );
            }
        }
        return $ids;
    }
    function get_data()
    {
        return $this->_category_group;
    }
    function exist($cid)
    {
        return isset( $this->_category_map[$cid] );
    }
    // 直接访问表
    function get_list_by_ids($ids)
    {
        // 为判断导航的二级菜单类型提供数据
        if (empty( $ids ))
        {
            return array ();
        }
        $this->_db->select( $this->_table, '`id`,`model`' );
        $this->_db->where( 'id in(?) and `deleted`=0', $ids );
        return $this->_db->fetch_all();
    }
    function get_sub_menus($fids)
    {
        // 导航二级菜单（多组一次取出）
        $result = array ();
        if (empty( $fids ))
        {
            return $result;
        }
        $this->_db->select( $this->_table, '`id`,`fid`,`img_cover`,`list_uri`,`title`,`subtitle`,`ext`' );
        $this->_db->where( 'fid in(?) and `deleted`=0', $fids );
        $this->_db->order( '`fid`,`sort` desc,`id`' );
        $category = $this->_db->fetch_all();
        foreach ( $category as &$item )
        {
            $result[$item['fid']][] = &$item;
        }
        return $result;
    }
    function get_content_model($uri)
    {
        $this->_db->select( $this->_table, '`gid`,`model`,`list_uri`,`list_tpl`,`content_tpl`' );
        $this->_db->where( '`fid`=0 and `deleted`=0 and (`list_uri`=? or `content_uri`=?)', array (
                $uri,
                $uri 
        ) );
        $this->_db->limit( 1 );
        return $this->_db->fetch_row();
    }
}